import { click } from "@testing-library/user-event/dist/click";

interface EventList {
  [propsName: string]: Function[];
}

// 封装 事件类
export class Event {
  eventList: EventList = {};
  // 绑定事件
  on(type: string, callback: Function) {
    if (this.eventList[type]) {
      this.eventList[type].push(callback);
    } else {
      this.eventList[type] = [callback];
    }
  }
  // 触发
  emit(type: string) {
    if (this.eventList[type]) {
      this.eventList[type].forEach((callback) => {
        callback();
      });
    }
  }
  // 解除
  off(type: string, callback: Function) {
    if (this.eventList[type]) {
      this.eventList[type] = this.eventList[type].filter((v) => v !== callback);
    }
  }
}

const ev = new Event();
const click1 = () => console.log(1);
const click2 = () => console.log(2);

ev.on("click", click1);
ev.on("click", click2);

ev.emit("click");

ev.off("click", click1);

ev.emit("click");

// const click1 = () => console.log(1)
// const click2 = () => console.log(2)
// const click3 = () => console.log(3)

// document.addEventListener('click', click1)

// document.addEventListener('click', click2)
// document.addEventListener('click', click3)

// document.body.click()
// 1
// 2
// 3

// document.removeEventListener('click', click1)
// document.removeEventListener('click', click2)

// document.body.click()
// document.body.click()

// interface OO {
//   name?: string
// }

// let o: OO = {

// }

// if(o.name) {
//   console.log(1)
// } else {
//   console.log(2)
// }
